import numpy as np
import matplotlib.pyplot as plt
from sklearn.svm import SVC

def load_file(input_file):
    X = []
    y = []
    with open(input_file,'r') as f:
        for line in f.readlines():
            data = [float(x) for x in line.split(',')]
            X.append(data[:-1])
            y.append(data[-1])

    X = np.array(X)
    y = np.array(y)

    return X,y


# 加载数据
input_file = "F:/python学习资料/Python-Machine-Learning-Cookbook-master/Chapter03/data_multivar.txt"

X,y = load_file(input_file)

# print(X[:5])
# print(y[:5])
# 把数据划分为训练集核测试集
from sklearn import model_selection
X_train,X_test,y_train,y_test = model_selection.train_test_split(X,y,test_size=0.25,random_state=5)
params = {'kernel':'rbf','probability':True}
classifier = SVC(**params,gamma='auto')
classifier.fit(X_train,y_train)
# 定义输入数据点
input_datapoints = np.array([[2,1.5],[8,9],[4.8,5.2],[4,4],[2.5,7],
                             [7.6,2],[5.4,5.9]])
# 计算数据点到边界的距离
print("Distance from the boundary")
for i in input_datapoints:
    print(i,'->',classifier.decision_function([i])[0])
    print(i,"->",classifier.predict_proba([i])[0])
    print("#########")